﻿namespace Deppton.Mobile.Views
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using Deppton.Model;

    public partial class WorkQueueForm : DepptonForm
    {
        public WorkQueueForm(IServicesPortfolio servicesPortfolio)
        {
            this.ServicesPortfolio = servicesPortfolio;
            this.InitializeComponent();
            this.Load += new EventHandler(this.WorkQueueFormLoad);
        }

        private IServicesPortfolio ServicesPortfolio
        {
            get;
            set;
        }

        private void WorkQueueFormLoad(object sender, System.EventArgs e)
        {
            this.UpdateInformation();
        }

        private void UpdateInformation()
        {
            this.UpdateInformation(this.ServicesPortfolio.SurveysStorageService);
        }

        private void UpdateInformation(ISurveysStorageService surveysStorageService)
        {
            var dataSource = surveysStorageService.RetrieveAll();

            this.WorkItemsListView.Items.Clear();

            foreach (var item in dataSource)
            {
                var viewItem = new ListViewItem(new[]
                    {
                        item.Description,
                        item.Status.ToFriendlyStatus()
                    });
                viewItem.Tag = item.Id;

                this.WorkItemsListView.Items.Add(viewItem);
            }
        }

        private void BackMenuItemClick(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void ContinueMenuItemClick(object sender, EventArgs e)
        {
            if (this.SelectedSurveyId != null)
            {
                using (var surveyForm = new SurveyForm(this.ServicesPortfolio, this.SelectedSurveyId))
                {
                    surveyForm.ShowDialog();
                }

                this.UpdateInformation();
            }
        }

        private string SelectedSurveyId
        {
            get
            {
                if (this.WorkItemsListView.SelectedIndices.Count != 1)
                {
                    return null;
                }

                return this.WorkItemsListView.Items[this.WorkItemsListView.SelectedIndices[0]].Tag as string;
            }
        }

        private void CreateNewMenuItemClick(object sender, EventArgs e)
        {
            using (var newSurveyForm = new NewSurveyForm(this.ServicesPortfolio))
            {
                if (newSurveyForm.ShowDialog() == DialogResult.OK)
                {
                    this.UpdateInformation();
                }
            }
        }

        private void DeleteMenuItemClick(object sender, EventArgs e)
        {
            if (this.SelectedSurveyId != null)
            {
                this.ServicesPortfolio.SurveysStorageService.Delete(this.SelectedSurveyId);

                this.UpdateInformation();
            }
        }
    }
}
